/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import vanScheduling.Driver;

/**
 *
 * @author Dan
 */
public class DriverDAO extends DAOTest {

    public synchronized static boolean addDriver(Driver driver)
    {
        Connection con = getConnection();
        boolean result = false;
        
        try {
            CallableStatement cs = con.prepareCall("{call dbo.stp_insDriver(?, ?, ?)}");
            cs.setString(1, driver.getName());
            cs.setString(2, driver.getEmail());
            cs.setString(3, driver.getPhoneNumber());
            int added = cs.executeUpdate();

            result = added > 0;

            cs.close();
            con.close();
        }
        catch (SQLException e) {e.printStackTrace();}

        return result;
    }

    public static List<Driver> getDrivers()
    {
        Connection con = getConnection();
        ArrayList<Driver> drivers = new ArrayList<Driver>();
        try {
            CallableStatement cs = con.prepareCall("{call stp_selAllDrivers}");
            ResultSet rs = cs.executeQuery();

            while (rs.next())
            {
                int id = rs.getInt("ID");
                String name = rs.getString("Name");
                String email = rs.getString("EMail");
                String phone = rs.getString("PhoneNumber");
                drivers.add(new Driver(id, name, email, phone));
            }

            cs.close();
            con.close();
        }
        catch (SQLException e) {System.err.println(e.getMessage());e.printStackTrace();}
        return drivers;
    }

    public synchronized static boolean deleteDriver(Driver driver)
    {
        Connection con = getConnection();
        boolean result = false;
        try
        {
            CallableStatement cs = con.prepareCall("{call stp_delDriver(?)}");
            cs.setInt(1, driver.getId());
            int delete = cs.executeUpdate();

            result = delete > 0;
        }
        catch (SQLException e) {System.err.println(e.getMessage());e.printStackTrace();}
        return result;
    }

    public static Driver getDriverByID(int driverId) {
        Connection con = getConnection();
        boolean result = false;
        Driver returnDriver = null;

        try {
            CallableStatement cs = con.prepareCall("{call dbo.stp_selDriverByID(?)}");
            cs.setInt(1, driverId);

            ResultSet rs = cs.executeQuery();

            while (rs.next())
            {
                int id = rs.getInt("ID");
                String name = rs.getString("Name");
                String email = rs.getString("EMail");
                String phone = rs.getString("PhoneNumber");
                returnDriver = new Driver(id, name, email, phone);
            }

            cs.close();
            con.close();
        }
        catch (SQLException e) {e.printStackTrace();}
        return returnDriver;
    }
}
